# -*- encoding: utf-8 -*-
"""
@File    : User_loginout_his.py
@Time    : 2020/3/23 3:26 下午
@Author  : david wang
@Email   : 3063737@qq.com
@Software: PyCharm
"""

# 导入插件
import pandas as pd
import numpy as np
from pandas import DataFrame

import setting

# 定义文件路径
iopath = setting.resource_file+'User_loginout_his.xlsx'
filename1 = setting.export_file+'User_loginout_his_month.html'
filename2 = setting.export_file+'User_loginout_his_time.html'

# 读取xls文件
df_month = pd.DataFrame(pd.read_excel(iopath, sheet_name='month_total'))
df_time = pd.DataFrame(pd.read_excel(iopath, sheet_name='time_total'))
# 获取数据
cols_month  = df_month.columns.values.tolist()  # 获取数据头信息
cols_time  = df_time.columns.values.tolist()  # 获取数据头信息
da0_month = [str(i)[0:10] for i in pd.Series.tolist(pd.to_datetime(df_month[cols_month[0]]))]
da0_time = [str(i)[0:10] for i in pd.Series.tolist(pd.to_datetime(df_time[cols_time[0]]))]
# da1 = pd.Series.tolist(df[cols[1]])
da1_month = pd.Series.tolist(df_month[cols_month[0]])
da2_month = pd.Series.tolist(df_month[cols_month[1]])

da1_time = pd.Series.tolist(df_time[cols_time[0]])
da2_time = pd.Series.tolist(df_time[cols_time[1]])
da3_time = pd.Series.tolist(df_time[cols_time[2]])
da4_time = pd.Series.tolist(df_time[cols_time[3]])
from pyecharts.charts import Bar
from pyecharts import options as opts
from pyecharts.globals import ThemeType

c = (
    Bar(init_opts=opts.InitOpts(theme=ThemeType.WHITE, height="800px", width="1500px"))
    .add_xaxis(da1_month)
    .add_yaxis(cols_month[1], da2_month,
               itemstyle_opts=opts.ItemStyleOpts(color='#2f4554'),
               label_opts=opts.LabelOpts(is_show=False), yaxis_index=1,
                       # category_gap设置间距
                       # category_gap="1%",
                       # is_selectedFalse默认不显示某商品
                       is_selected=True,
                       markline_opts=opts.MarkLineOpts(data=[opts.MarkLineItem(type_="average", name="平均值")]),
                       markpoint_opts=opts.MarkPointOpts(data=[opts.MarkPointItem(type_="max", name="最大值"),
                                                                opts.MarkPointItem(type_="min", name="最大值"),
                                                                opts.MarkPointItem(type_="average", name="平均值"), ])
                       )
     .extend_axis(
            yaxis=opts.AxisOpts(name="注销数/个", position='left'))
    .set_global_opts(title_opts=opts.TitleOpts(title="注销用户数月汇总", subtitle="按月统计"),
         # toolbox_opts工具栏显示
         toolbox_opts=opts.ToolboxOpts(),
         # legend_opts子项显示
         legend_opts=opts.LegendOpts(is_show=True),
         # 增加单位
         yaxis_opts=opts.AxisOpts(
         axislabel_opts=opts.LabelOpts(formatter="{value} /个")),
    )

    .render(filename1)
)

c_time = (
    Bar(init_opts=opts.InitOpts(theme=ThemeType.WHITE, height="800px", width="1500px"))
    .add_xaxis(da1_time)
    .add_yaxis(cols_time[1], da2_time,
               itemstyle_opts=opts.ItemStyleOpts(color='#2f4554'),
               label_opts=opts.LabelOpts(is_show=True), yaxis_index=0,
                       # category_gap设置间距
                       # category_gap="1%",
                       # is_selectedFalse默认不显示某商品
                       is_selected=True,
                       markline_opts=opts.MarkLineOpts(data=[opts.MarkLineItem(type_="average", name="平均值")]),
                       markpoint_opts=opts.MarkPointOpts(data=[opts.MarkPointItem(type_="max", name="最大值"),
                                                                opts.MarkPointItem(type_="min", name="最大值"),
                                                                opts.MarkPointItem(type_="average", name="平均值"), ])
                       )
     .add_yaxis(cols_time[2], da3_time,itemstyle_opts=opts.ItemStyleOpts(color='#c23531'),)
     .add_yaxis(cols_time[3], da4_time,itemstyle_opts=opts.ItemStyleOpts(color='#61a0a8'),)
     .extend_axis(
            yaxis=opts.AxisOpts(name="注销数/个", position='left'))
    .set_global_opts(title_opts=opts.TitleOpts(title="注销时间段统计", subtitle="按小时统计"),
         # toolbox_opts工具栏显示
         toolbox_opts=opts.ToolboxOpts(),
         # legend_opts子项显示
         legend_opts=opts.LegendOpts(is_show=True),
         # 增加单位
         yaxis_opts=opts.AxisOpts(
         axislabel_opts=opts.LabelOpts(formatter="{value} /个")),
    )

    .render(filename2)
)

import webbrowser
webbrowser.open_new_tab( 'file://'+filename1)
webbrowser.open_new_tab( 'file://'+filename2)
